package com.example.demo.controller;


import com.example.demo.entity.pojo.Orders;
import com.example.demo.service.IOrdersService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;

    @GetMapping("/sel")
    public R sel() {
        List<Orders> orders = ordersService.list();
        Map<String, Object> map = new HashMap<>();
        map.put("data", orders);
        map.put("msg", "查询成功");
        map.put("code", 200);
        return R.ok(map);
    }

    @GetMapping("/selByUser")
    public R selByUser(long id) {
        List<Orders> orders = ordersService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Orders>().eq("userid", id));
        Map<String, Object> map = new HashMap<>();
        map.put("data", orders);
        map.put("msg", "查询成功");
        map.put("code", 200);
        return R.ok(map);
    }

    @PostMapping("/add")
    public R add(@RequestBody  Orders orders) {
        ordersService.save(orders);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "添加成功");
        map.put("code", 200);
        return R.ok(map);
    }

    @PostMapping("/del")
    public R del(long id) {
        ordersService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "删除成功");
        map.put("code", 200);
        return R.ok(map);
    }

    @PostMapping("/upd")
    public R upd(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "修改成功");
        map.put("code", 200);
        return R.ok();
    }
}
